<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: Technoweenie::AttachmentFu::ActMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">Technoweenie::AttachmentFu::ActMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu_rb.html">
                vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000062">has_attachment</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="InstanceMethods.html">InstanceMethods</a></span>
        <span class="include-name"><a href="Backends.html">Technoweenie::AttachmentFu::Backends</a></span>
        <span class="include-name"><a href="Processors.html">Technoweenie::AttachmentFu::Processors</a></span>
        <span class="include-name"><a href="Processors.html">Technoweenie::AttachmentFu::Processors</a></span>
      </div>
    </div>

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000062" class="method-detail">
        <a name="M000062"></a>

        <div class="method-heading">
          <a href="#M000062" class="method-signature">
          <span class="method-name">has_attachment</span><span class="method-args">(options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Options:
</p>
<ul>
<li><tt>:content_type</tt> - Allowed content types. Allows all by default. Use
:image to allow all standard image types.

</li>
<li><tt>:min_size</tt> - Minimum size allowed. 1 byte is the default.

</li>
<li><tt>:max_size</tt> - Maximum size allowed. 1.megabyte is the default.

</li>
<li><tt>:size</tt> - Range of sizes allowed. (1..1.megabyte) is the default.
This overrides the :min_size and :max_size options.

</li>
<li><tt>:resize_to</tt> - Used by RMagick to resize images. Pass either an
array of width/height, or a geometry string.

</li>
<li><tt>:thumbnails</tt> - Specifies a set of thumbnails to generate. This
accepts a hash of filename suffixes and RMagick resizing options.

</li>
<li><tt>:thumbnail_class</tt> - Set what class to use for thumbnails. This
attachment class is used by default.

</li>
<li><tt>:path_prefix</tt> - path to store the uploaded files. Uses
public/#{table_name} by default for the filesystem, and just #{table_name}

<pre>
  for the S3 backend.  Setting this sets the :storage to :file_system.
</pre>
</li>
<li><tt>:storage</tt> - Use :file_system to specify the attachment data is
stored with the file system. Defaults to :db_system.

</li>
</ul>
<p>
Examples:
</p>
<pre>
  has_attachment :max_size =&gt; 1.kilobyte
  has_attachment :size =&gt; 1.megabyte..2.megabytes
  has_attachment :content_type =&gt; 'application/pdf'
  has_attachment :content_type =&gt; ['application/pdf', 'application/msword', 'text/plain']
  has_attachment :content_type =&gt; :image, :resize_to =&gt; [50,50]
  has_attachment :content_type =&gt; ['application/pdf', :image], :resize_to =&gt; 'x50'
  has_attachment :thumbnails =&gt; { :thumb =&gt; [50, 50], :geometry =&gt; 'x50' }
  has_attachment :storage =&gt; :file_system, :path_prefix =&gt; 'public/files'
  has_attachment :storage =&gt; :file_system, :path_prefix =&gt; 'public/files',
    :content_type =&gt; :image, :resize_to =&gt; [50,50]
  has_attachment :storage =&gt; :file_system, :path_prefix =&gt; 'public/files',
    :thumbnails =&gt; { :thumb =&gt; [50, 50], :geometry =&gt; 'x50' }
  has_attachment :storage =&gt; :s3
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000062-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000062-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb, line 39</span>
39:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">has_attachment</span>(<span class="ruby-identifier">options</span> = {})
40:         <span class="ruby-comment cmt"># this allows you to redefine the acts' options for each subclass, however</span>
41:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:min_size</span>]         <span class="ruby-operator">||=</span> <span class="ruby-value">1</span>
42:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:max_size</span>]         <span class="ruby-operator">||=</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">megabyte</span>
43:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:size</span>]             <span class="ruby-operator">||=</span> (<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:min_size</span>]<span class="ruby-operator">..</span><span class="ruby-identifier">options</span>[<span class="ruby-identifier">:max_size</span>])
44:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:thumbnails</span>]       <span class="ruby-operator">||=</span> {}
45:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:thumbnail_class</span>]  <span class="ruby-operator">||=</span> <span class="ruby-keyword kw">self</span>
46:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:s3_access</span>]        <span class="ruby-operator">||=</span> <span class="ruby-identifier">:public_read</span>
47:         <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:content_type</span>] = [<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:content_type</span>]].<span class="ruby-identifier">flatten</span>.<span class="ruby-identifier">collect!</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span> <span class="ruby-identifier">t</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">:image</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Technoweenie</span><span class="ruby-operator">::</span><span class="ruby-constant">AttachmentFu</span>.<span class="ruby-identifier">content_types</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">t</span> }.<span class="ruby-identifier">flatten</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:content_type</span>].<span class="ruby-identifier">nil?</span>
48:         
49:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:thumbnails</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
50:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;:thumbnails option should be a hash: e.g. :thumbnails =&gt; { :foo =&gt; '50x50' }&quot;</span>
51:         <span class="ruby-keyword kw">end</span>
52:         
53:         <span class="ruby-comment cmt"># doing these shenanigans so that #attachment_options is available to processors and backends</span>
54:         <span class="ruby-identifier">class_inheritable_accessor</span> <span class="ruby-identifier">:attachment_options</span>
55:         <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">attachment_options</span> = <span class="ruby-identifier">options</span>
56: 
57:         <span class="ruby-comment cmt"># only need to define these once on a class</span>
58:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">included_modules</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">InstanceMethods</span>)
59:           <span class="ruby-identifier">attr_accessor</span> <span class="ruby-identifier">:thumbnail_resize_options</span>
60: 
61:           <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:storage</span>]     <span class="ruby-operator">||=</span> (<span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:file_system_path</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:path_prefix</span>]) <span class="ruby-operator">?</span> <span class="ruby-identifier">:file_system</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">:db_file</span>
62:           <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:path_prefix</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:file_system_path</span>]
63:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:path_prefix</span>].<span class="ruby-identifier">nil?</span>
64:             <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:path_prefix</span>] = <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:storage</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">:s3</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">table_name</span> <span class="ruby-operator">:</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;public&quot;</span>, <span class="ruby-identifier">table_name</span>)
65:           <span class="ruby-keyword kw">end</span>
66:           <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:path_prefix</span>]   = <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:path_prefix</span>][<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:path_prefix</span>].<span class="ruby-identifier">first</span> <span class="ruby-operator">==</span> <span class="ruby-value str">'/'</span>
67: 
68:           <span class="ruby-identifier">with_options</span> <span class="ruby-identifier">:foreign_key</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'parent_id'</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">m</span><span class="ruby-operator">|</span>
69:             <span class="ruby-identifier">m</span>.<span class="ruby-identifier">has_many</span>   <span class="ruby-identifier">:thumbnails</span>, <span class="ruby-identifier">:class_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:thumbnail_class</span>].<span class="ruby-identifier">to_s</span>
70:             <span class="ruby-identifier">m</span>.<span class="ruby-identifier">belongs_to</span> <span class="ruby-identifier">:parent</span>, <span class="ruby-identifier">:class_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">base_class</span>.<span class="ruby-identifier">to_s</span>
71:           <span class="ruby-keyword kw">end</span>
72:           <span class="ruby-identifier">before_destroy</span> <span class="ruby-identifier">:destroy_thumbnails</span>
73: 
74:           <span class="ruby-identifier">before_validation</span> <span class="ruby-identifier">:set_size_from_temp_path</span>
75:           <span class="ruby-identifier">after_save</span> <span class="ruby-identifier">:after_process_attachment</span>
76:           <span class="ruby-identifier">after_destroy</span> <span class="ruby-identifier">:destroy_file</span>
77:           <span class="ruby-identifier">extend</span>  <span class="ruby-constant">ClassMethods</span>
78:           <span class="ruby-identifier">include</span> <span class="ruby-constant">InstanceMethods</span>
79:           <span class="ruby-identifier">include</span> <span class="ruby-constant">Technoweenie</span><span class="ruby-operator">::</span><span class="ruby-constant">AttachmentFu</span><span class="ruby-operator">::</span><span class="ruby-constant">Backends</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-node">&quot;#{options[:storage].to_s.classify}Backend&quot;</span>)
80:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">attachment_options</span>[<span class="ruby-identifier">:processor</span>]
81:             <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:none</span>
82:             <span class="ruby-keyword kw">when</span> <span class="ruby-keyword kw">nil</span>
83:               <span class="ruby-identifier">processors</span> = <span class="ruby-constant">Technoweenie</span><span class="ruby-operator">::</span><span class="ruby-constant">AttachmentFu</span>.<span class="ruby-identifier">default_processors</span>.<span class="ruby-identifier">dup</span>
84:               <span class="ruby-keyword kw">begin</span>
85:                 <span class="ruby-identifier">include</span> <span class="ruby-constant">Technoweenie</span><span class="ruby-operator">::</span><span class="ruby-constant">AttachmentFu</span><span class="ruby-operator">::</span><span class="ruby-constant">Processors</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-node">&quot;#{processors.first}Processor&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">processors</span>.<span class="ruby-identifier">any?</span>
86:               <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">LoadError</span>, <span class="ruby-constant">MissingSourceFile</span>
87:                 <span class="ruby-identifier">processors</span>.<span class="ruby-identifier">shift</span>
88:                 <span class="ruby-keyword kw">retry</span>
89:               <span class="ruby-keyword kw">end</span>
90:             <span class="ruby-keyword kw">else</span>
91:               <span class="ruby-keyword kw">begin</span>
92:                 <span class="ruby-identifier">include</span> <span class="ruby-constant">Technoweenie</span><span class="ruby-operator">::</span><span class="ruby-constant">AttachmentFu</span><span class="ruby-operator">::</span><span class="ruby-constant">Processors</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-node">&quot;#{options[:processor].to_s.classify}Processor&quot;</span>)
93:               <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">LoadError</span>, <span class="ruby-constant">MissingSourceFile</span>
94:                 <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;Problems loading #{options[:processor]}Processor: #{$!}&quot;</span>
95:               <span class="ruby-keyword kw">end</span>
96:           <span class="ruby-keyword kw">end</span>
97:           <span class="ruby-identifier">after_validation</span> <span class="ruby-identifier">:process_attachment</span>
98:         <span class="ruby-keyword kw">end</span>
99:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>